﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Solidinfo.Web.Helpers
{
    public static class ControllerExtensions
    {
        public static List<ModelPropertyValidateError> GetModelValidateErrors(this Controller controller)
        {
            List<ModelPropertyValidateError> list = new List<ModelPropertyValidateError>();
            foreach (var item in controller.ModelState)
            {
                if (item.Value.Errors != null && item.Value.Errors.Count > 0)
                {
                    ModelPropertyValidateError validateError = new ModelPropertyValidateError(item.Key);
                    foreach (var modelError in item.Value.Errors)
                    {
                        validateError.Errors.Add(modelError.ErrorMessage);
                    }
                    list.Add(validateError);
                }
            }
            return list;
        }
        
    }

    public class ModelValiateError
    {

    }
    /// <summary>
    /// 模型属性验证错误信息
    /// </summary>
    public class ModelPropertyValidateError
    {
        public string PropertyName { get; private set; }
        public List<ModelError> Errors { get; private set; }
        public ModelPropertyValidateError(string propertyName)
        {
            PropertyName = propertyName;
            Errors = new List<ModelError>();
        }
    }
    /// <summary>
    /// 模型错误信息
    /// </summary>
    public class ModelError
    {
        public string Message { get; set; }
        public ModelError(string errorMsg)
        {
            Message = errorMsg;
        }
        public static implicit operator ModelError(string d)
        {
            return new ModelError(d);
        }
        public static implicit operator string(ModelError d)
        {
            return d.Message;
        }
    }
}